var mongoose = require('./model');
var productSchema = new mongoose.Schema({
    productNum: {type: String, unique: true},
    productName: {type: String},
    productImg: {type: String},
    marketPrice: {type: Number},
    relPrice: {type: Number},
    productAmout: {type: Number},
    productStyle: {type: String},
    productMinor: {type: String}
});
var productModel = mongoose.model('product', productSchema);
exports.getProducts = function (currentPage, callback) {
    var pageNum = 20;
    if (currentPage < 1) {
        currentPage = 1;
    }
    var startPositon = (currentPage - 1) * pageNum;

    productModel.find(function (err, doc) {
        if (err)
            throw err;
        if (doc.length > 0) {
            var totalPage = Math.ceil(doc.length / pageNum);
            if (currentPage > totalPage) {
                currentPage = totalPage;
            }
            callback(doc, totalPage + 1);
        } else {
            callback(doc, '产品数据获取失败')
        }
    }).limit(pageNum).skip(startPositon);
};
exports.getProductsByStyle = function (currentPage, style, callback) {
    var pageNum = 10;
    if (currentPage < 1) {
        currentPage = 1;
    }
    var startPositon = (currentPage - 1) * pageNum;
    productModel.find({productStyle: style}, function (err, doc) {
        if (err)
            throw err;
        if (doc.length > 0) {
            var totalPage = Math.ceil(doc.length / pageNum);
            if (currentPage > totalPage) {
                currentPage = totalPage;
            }
            callback(doc, totalPage);
        } else {
            callback(doc, '产品数据获取失败')
        }
    }).limit(pageNum).skip(startPositon);
};
exports.getProductsByNum = function (num, callback) {
    productModel.find(num, function (err, doc) {
        if (err)
            throw err;
        if (doc.length > 0) {
            callback(doc);
        } else {
            callback(doc, '详情数据获取失败')
        }
    })
}
